package cn.iocoder.yudao.module.system.service.erp.warehouseoutsuk;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseoutsuk.vo.WarehouseOutSukPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseoutsuk.vo.WarehouseOutSukSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseoutsuk.WarehouseOutSukDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.warehouseoutsuk.WarehouseOutSukMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WAREHOUSE_OUT_SUK_NOT_EXISTS;

/**
 * 出库单sku Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseOutSukServiceImpl implements WarehouseOutSukService {

    @Resource
    private WarehouseOutSukMapper warehouseOutSukMapper;

    @Override
    public Long createWarehouseOutSuk(WarehouseOutSukSaveReqVO createReqVO) {
        // 插入
        WarehouseOutSukDO warehouseOutSuk = BeanUtils.toBean(createReqVO, WarehouseOutSukDO.class);
        warehouseOutSukMapper.insert(warehouseOutSuk);
        // 返回
        return warehouseOutSuk.getId();
    }

    @Override
    public void updateWarehouseOutSuk(WarehouseOutSukSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseOutSukExists(updateReqVO.getId());
        // 更新
        WarehouseOutSukDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseOutSukDO.class);
        warehouseOutSukMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseOutSuk(Long id) {
        // 校验存在
        validateWarehouseOutSukExists(id);
        // 删除
        warehouseOutSukMapper.deleteById(id);
    }

    private void validateWarehouseOutSukExists(Long id) {
        if (warehouseOutSukMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_OUT_SUK_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseOutSukDO getWarehouseOutSuk(Long id) {
        return warehouseOutSukMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseOutSukDO> getWarehouseOutSukPage(WarehouseOutSukPageReqVO pageReqVO) {
        return warehouseOutSukMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseOutSukDO> getOutWarehouseIdSukList(Long outWarehouseId) {
        return warehouseOutSukMapper.selectList(WarehouseOutSukDO::getOutWarehouseId,outWarehouseId);
    }

    @Override
    public void deleteWarehouseOutSuks(Long outWarehouseId) {
        warehouseOutSukMapper.delete(WarehouseOutSukDO::getOutWarehouseId,outWarehouseId);
    }

}